﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;


namespace 药品信息管理
{
    public partial class YP_PZWH : Form
    {
        public YP_PZWH()
        {
            InitializeComponent();
        }
        int AddOrEdit;//标示是添加或者修改记录，添加记录时其值为0，修改记录时其值为1
        SqlConnection conn;
        SqlCommand com;
        SqlDataAdapter da;
        int firsttime1 = 0;
        DataSet ds;

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is Button) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        //private void FillDateGridView()//自定义方法，填充表格数据
        //{
        //    da = new SqlDataAdapter("SELECT PZWH 批准文号, YPBM 药品编码, ZCBM 字典名称, TYMC 通用名称, HXMC 化学名称, LDMC 拉丁名称, SCCS 生产厂商, YXQX 有限期限, ZCSB 注册商标 FROM  YK_ZDPZWH", conn);
        //    DataSet ds = new DataSet();
        //    try
        //    {
        //        if (conn.State == ConnectionState.Closed)
        //        {
        //            conn.Open();
        //        }
        //        int count = da.Fill(ds, "YK_ZDPZWH");
        //        conn.Close();
        //        dataGridViewX1.DataSource = ds.Tables["YK_ZDPZWH"];
                
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message.ToString());
        //    }
        //}

        private void ClearText()//自定义方法，清除文本框内容
        {

            textBoxX1.Clear();
            textBoxX2.Clear();
            textBoxX3.Clear();
            textBoxX4.Clear();
            textBoxX5.Clear();
            textBoxX6.Clear();
            textBoxX7.Clear();
            textBoxX8.Clear();
            textBoxX9.Clear();
            textBoxX10.Clear();
        }

        private void LockedTextBox()//自定义方法，锁定文本框
        {
            textBoxX1.Enabled = false;
            textBoxX2.Enabled = false;
            textBoxX3.Enabled = false;
            textBoxX4.Enabled = false;
            textBoxX5.Enabled = false;
            textBoxX6.Enabled = false;
            textBoxX7.Enabled = false;
            textBoxX8.Enabled = false;
            textBoxX9.Enabled = false;
            textBoxX10.Enabled = false;
        }

        private void UnLockedTextBox()//自定义方法，解除锁定文本框
        {
            textBoxX1.Enabled = true;
            textBoxX2.Enabled = true;
            textBoxX3.Enabled = true;
            textBoxX4.Enabled = true;
            textBoxX5.Enabled = true;
            textBoxX6.Enabled = true;
            textBoxX7.Enabled = true;
            textBoxX8.Enabled = true;
            textBoxX9.Enabled = true;
            textBoxX10.Enabled = true;
        }
        private void YP_PZWH_Load(object sender, EventArgs e)
        {
            db db1 = new db();
            string str = (@"SELECT PZWH 批准文号, YPBM 药品编码, ZCBM 字典名称, TYMC 通用名称, HXMC 化学名称, LDMC 拉丁名称, SCCS 生产厂商, YXQX 有限期限, ZCSB 注册商标 FROM  YK_ZDPZWH");
            dataGridViewX1.DataSource = db1.sqldt(str);
            LockedTextBox();
            //保存按钮无效
            buttonItem1.Enabled = false;
            //取消按钮无效
            buttonItem5.Enabled = false;

        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            LockedTextBox();
            buttonItem5.Enabled = false;
            buttonItem1.Enabled = false;
        }

        private void dataGridViewX1_Click(object sender, EventArgs e)
        {
            //单击表格中的单元格时触发该事件
            textBoxX1.Text = Convert.ToString(dataGridViewX1[0, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX2.Text = Convert.ToString(dataGridViewX1[1, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX3.Text = Convert.ToString(dataGridViewX1[2, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX5.Text = Convert.ToString(dataGridViewX1[3, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX6.Text = Convert.ToString(dataGridViewX1[4, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX7.Text = Convert.ToString(dataGridViewX1[5, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX8.Text = Convert.ToString(dataGridViewX1[6, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX9.Text = Convert.ToString(dataGridViewX1[7, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
            textBoxX10.Text = Convert.ToString(dataGridViewX1[8, dataGridViewX1.CurrentCell.RowIndex].Value).Trim();
        }



        private void buttonItem1_Click(object sender, EventArgs e)
        {
            db db1 = new db();
            if (textBoxX1.Text == "")
            {
                MessageBox.Show("批准文号不能为空！请输入再保存！");
                return;
            }
            else
            {
                string sqlcount = db1.sqlstr("select count(*) from YK_ZDPZWH where PZWH = '" + textBoxX1.Text + "'");
                if (sqlcount != "0")
                { MessageBox.Show("批准文号重复"); }
                else
                {
                    db1.sqlcmd(@"insert into YK_ZDPZWH (PZWH, YPBM, ZCBM, TYMC, HXMC, LDMC, SCCS, YXQX, ZCSB) values( '" + textBoxX1.Text + "', '" + textBoxX2.Text + "', '" + textBoxX3.Text + "','" + textBoxX5.Text + "','" + textBoxX6.Text + "','" + textBoxX7.Text + "','" + textBoxX8.Text + "','" + textBoxX9.Text + "','" + textBoxX10.Text + "')");
                    MessageBox.Show("插入成功");
                }
                YP_PZWH_Load(sender,e);
            }
            
#region
            //if (AddOrEdit == 0)
            //{
            //    //添加记录后的保存
            //    try
            //    {
            //        //判断批准文号是否存在
            //        da = new SqlDataAdapter("select * from YK_ZDPZWH where PZWH='" + textBoxX1.Text + "'",conn);
            //        DataSet ds = new DataSet();
            //        if (conn.State == ConnectionState.Closed)
            //        {
            //            conn.Open();
            //        }
            //        int count = da.Fill(ds, "YK_ZDPZWH");
            //        conn.Close();
            //        if (count != 0)
            //        {
            //            MessageBox.Show("该批准文号已经存在，请重新输入");
            //            textBoxX1.Focus();
            //            return;
            //        }
            //        com = new SqlCommand("insert into YK_ZDPZWH(PZWH,ZCBM,YPBM,TYMC,HXMC,LDMC,SCCS,YXQX,ZCSB)values('" + textBoxX1.Text.Trim() + "','" + textBoxX2.Text.Trim() + "','" + textBoxX3.Text.Trim() + "','" + textBoxX5.Text.Trim() + "','" + textBoxX6.Text.Trim() + "','" + textBoxX7.Text.Trim() + "','" + textBoxX8.Text.Trim() + "','" + textBoxX9.Text.Trim() + "','" + textBoxX10.Text.Trim() + "')", conn);
            //        if (conn.State == ConnectionState.Closed)
            //        {
            //            conn.Open();
            //        }
            //        com.ExecuteNonQuery();
            //        conn.Close();
            //        MessageBox.Show("添加批准文号成功");
            //        FillDateGridView();
            //        LockedTextBox();
            //        buttonItem5.Enabled = false;
            //        buttonItem1.Enabled = false;
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message.ToString());
            //    }
            //}
            //if (AddOrEdit == 1)
            //{
            //    //修改记录后保存
            //    try
            //    {
            //        com = new SqlCommand("Update YK_ZDPZWH set PZWH='" + textBoxX1.Text.Trim() + "',ZCBM='" + textBoxX2.Text.Trim() + "',YPBM='" + textBoxX3.Text.Trim() + "',TYMC='" + textBoxX5.Text.Trim() + "',HXMC='" + textBoxX6.Text.Trim() + "',LDMC='" + textBoxX7.Text.Trim() + "',SCCS='" + textBoxX8.Text.Trim() + "',YXQX='" + textBoxX9.Text.Trim() + "',ZCSB='" + textBoxX10.Text.Trim() + "' where ID=" + Convert.ToString(dataGridViewX1[0, dataGridViewX1.CurrentCell.RowIndex].Value).Trim(), conn);
            //        if (conn.State == ConnectionState.Closed)
            //        {
            //            conn.Open();
            //        }
            //        com.ExecuteNonQuery();
            //        conn.Close();
            //        MessageBox.Show("修改批准文号成功！");
            //        FillDateGridView();
            //        LockedTextBox();
            //        buttonItem5.Enabled = false;
            //        buttonItem1.Enabled = false;
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message.ToString());
            //    }
            //}
#endregion
        }
        private void buttonItem2_Click(object sender, EventArgs e)
        {
            buttonItem1.Enabled = true;
            buttonItem5.Enabled = true;
            dataGridView1.Visible = false;
            ClearText();
            UnLockedTextBox();
            textBoxX1.Focus();
        }
        private void buttonItem3_Click(object sender, EventArgs e)
        {
            buttonItem5.Enabled = true;
            buttonItem1.Enabled = true;
            //AddOrEdit = 1;
            ////ClearText();
            UnLockedTextBox();
            textBoxX1.Focus();



        }

        private void buttonItem4_Click(object sender, EventArgs e)
        {
            db db1 = new db();
            if (MessageBox.Show("确定要删除该供应商信息吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                db1.sqlcmd(@"delete from YK_ZDPZWH where pzwh='" + dataGridViewX1.CurrentRow.Cells[0].Value.ToString() + "'");
            }
            YP_PZWH_Load(sender, e);
#region
            //if (MessageBox.Show("确定要删除该系信息吗？", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            //{
            //    try
            //    {
            //        com = new SqlCommand("delete from YK_ZDPZWH where ID=" +
            //            Convert.ToString(dataGridViewX1[0, dataGridViewX1.CurrentCell.RowIndex].Value).Trim(), conn);
            //        if (conn.State == ConnectionState.Closed)
            //        {
            //            conn.Open();
            //        }
            //        com.ExecuteNonQuery();
            //        conn.Close();
            //        MessageBox.Show("删除数据成功！");
            //        ClearText();
            //        FillDateGridView();
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message.ToString());
            //    }
            //}
            #endregion
        }

        private void textBoxItem1_InputTextChanged(object sender)
        {
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                DataTable dt_pzwh = new DataTable();
                SqlCommand pzwh_dw = new SqlCommand("SELECT  PZWH 批准文号, YPBM 药品编码, ZCBM 字典名称, TYMC 通用名称, HXMC 化学名称, LDMC 拉丁名称, SCCS 生产厂商, YXQX 有限期限, ZCSB 注册商标 FROM  YK_ZDPZWH WHERE ID LIKE'%" + textBoxItem1.Text.ToString() + "%' OR PZWH LIKE'%" + textBoxItem1.Text.ToString() + "%' OR YPBM LIKE'%" + textBoxItem1.Text.ToString() + "%'",con);//sql语句
                da.SelectCommand = pzwh_dw;//设置为已实例化SqlDataAdapter的查询命令
                da.Fill(dt_pzwh);
                dataGridViewX1.DataSource = dt_pzwh;

            }
        }

        private void textBoxX3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                getSqlConnection myconnection = new 药品信息管理.getSqlConnection();
                SqlConnection conn = myconnection.GetCon();
                string sql = "SELECT BM 药品编码, MC 名称, GG 规格, BZDW 包装单位 FROM  ZD_YP where BM='" + textBoxX3.Text.Trim() + "'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    textBoxX3.Text = myreader.GetString(0);
                    textBoxX4.Text = Convert.ToString(myreader.GetString(1));

                }
                this.dataGridView1.Top = this.textBoxX3.Top + this.textBoxX3.Height;

                if (firsttime1 == 0)
                {
                    firsttime1 = 1;
                    getData();
                }

                if (textBoxX3.Text == " ")
                {
                    if (ds.Tables[0].DefaultView.Count != 0)
                        dataGridView1.Visible = true;
                }

                ds.Tables[0].DefaultView.RowFilter = "药品编码 like'" + textBoxX3.Text.Trim() + "%'";

                if (ds.Tables[0].DefaultView.Count != 0)
                {
                    dataGridView1.Visible = true;
                    dataGridView1.CurrentCell = dataGridView1[0, 0];
                }

                if (ds.Tables[0].DefaultView.Count == 0)
                    dataGridView1.Visible = false;
            }

            catch (Exception ex)
            {
                MessageBox.Show("请检查" + ex.ToString());
                Application.Exit();
            }
        }

        private void getData()
        {
            getSqlConnection myconnection = new 药品信息管理.getSqlConnection();
            SqlConnection conn = myconnection.GetCon();
            try
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT BM 药品编码, MC 名称, GG 规格, BZDW 包装单位 FROM  ZD_YP", conn);
                ds = new DataSet();
                da.Fill(ds);
            }
            catch (Exception ex)
            {
                MessageBox.Show("请检查数据库连接!" + ex.ToString());
                Application.Exit();
            }
            finally
            {
                conn.Close();
            }
            this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.textBoxX3.Text = this.dataGridView1.CurrentCell.Value.ToString();
            this.dataGridView1.Visible = false;
        }


        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.textBoxX3.Text = this.dataGridView1.CurrentCell.Value.ToString();
                this.dataGridView1.Visible = false;

            }
        }

       

        
        }


    }